using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Tarea3
{
    partial class ViewGUI : Form, ViewInterface, ObserverInterface
    {
        private bool initialized;
        private int Row = 0;
        private int Col = 0;       
        private ControllerInterface controller;
        private ModelInterface model;
        PictureBox[] holder; 
        Label[] labels;
        Image queen;
        TableLayoutPanel tlpChessboard;
        
        
        public ViewGUI()
        {     
            InitializeComponent();
            queen = imagenes.Images[0];
            Form.CheckForIllegalCrossThreadCalls = false;
        }

        public void Initialize(string title, ControllerInterface controller)
        {            
            this.controller = controller;
            this.Text = title;
            this.cmbModel.SelectedIndex = 0;
            this.cmbSize.SelectedIndex = 0;
            initialized = true;
            selectBehavior();
            Application.Run((Form)this);
        }

        public void SetModel(string title,ModelInterface model)
        {
            if (this.model != null)
                model.RemoveObserver(this);

            this.model = model;
            this.Text = title;

            model.RegisterObserver(this);

            Win.Visible = false;
            lblMessage.Text = "";
            lblInfo.Text = "";

            int t = model.Size;
            chessboard(t);
            tlpChessboard.Hide();
            fillWithPics(t);
            tlpChessboard.Refresh();
        }

        public void Update(StateInterface state, string msg, string info) 
        {
            if (state.Index == model.Size)
                Win.Visible = true;
            else
                Win.Visible = false;

            lblMessage.Text = msg;
            lblInfo.Text = info;

            int counter = 0;
              for (int i = 0; i < model.Size; i++)
                  for (int j = 0; j < model.Size; j++)
                  {
                      if (state.Board[i,j] == 0)
                      {
                          holder[counter].Image = null;
                          labels[counter].Text = "";
                          //if (controller.IsRunning()) Squares[i * state.Board.GetLength + j].Image = imlImages.Images[0];
                      }
                      else if (state.Board[i,j] == -1)
                      {
                          //if (controller.IsRunning()) Squares[i * state.Board.GetLength + j].Image = null;
                          holder[counter].Image = null;
                          labels[counter].Text = "*";
                          //Console.Write("* " + counter + "  ");
                      }
                      else
                      {
                          //if (controller.IsRunning()) Squares[i * state.Board.GetLength + j].Image = null;
                          holder[counter].Image = queen;
                          labels[counter].Text = state.Board[i, j] + "";
                      }

                      counter++;
                  }
        }

        public void chessboard(int size)
        {
            tlpChessboard = new TableLayoutPanel();
            
            this.tlpChessboard.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
            | System.Windows.Forms.AnchorStyles.Left)
            | System.Windows.Forms.AnchorStyles.Right)));
            this.tlpChessboard.AutoSize = false;
            this.tlpChessboard.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.Inset;
            this.tlpChessboard.ColumnCount = size;
            for (int i = 0; i < tlpChessboard.ColumnCount; i++)
            {
                this.tlpChessboard.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, ((tlpChessboard.Width/size)*100)/tlpChessboard.Width));
            }
            this.tlpChessboard.Location = new System.Drawing.Point(1, 1);
            this.tlpChessboard.Name = "tlpChessboard";
            this.tlpChessboard.RowCount = size;
            for (int i = 0; i < tlpChessboard.ColumnCount; i++)
            {
                this.tlpChessboard.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, ((tlpChessboard.Height/size)*100)/tlpChessboard.Width));
            }
            this.tlpChessboard.Size = new System.Drawing.Size(590, 590);
            this.tlpChessboard.TabIndex = 0;
            this.panel1.Controls.Clear();
            this.panel1.Controls.Add(tlpChessboard);
        }
       

        private void chessBoard_Click(object sender, EventArgs e) 
        {
            
            //holder[Row * model.Size + Col].Image = null;
            //labels[Row * model.Size + Col].Text = "";

            for (int i = 0; i < holder.Length; i++) 
            { 
                if (sender.Equals(holder[i]))
                {                    
                    Row = i / model.Size;
                    Col = i % model.Size;
                    lbCol.Text = Col + "";
                    lbRow.Text = Row + "";
                    //holder[i].Image = queen;                                  
                    break;
                }                    
            }

            controller.SetPosition(Row, Col);

        }        
  
        private void fillWithPics(int n)
        {
            bool isEven = true;
            tlpChessboard.Controls.Clear();          
                        
            holder = new PictureBox[n*n];
            labels = new Label[n * n];
            
            for (int i = 0; i < n*n; i++)
            {                
                    holder[i] = new PictureBox();
                    holder[i].Click += new System.EventHandler(chessBoard_Click);
                    labels[i] = new Label();
                    holder[i].Controls.Add(labels[i]);
                    labels[i].ForeColor = System.Drawing.Color.Blue;
                    labels[i].AutoSize = true;
                    labels[i].BackColor = System.Drawing.Color.Transparent;
                    labels[i].Font = new System.Drawing.Font("Eurostile", 20);

                    
                    if (n % 2 == 0 && i % n == 0)
                        isEven = !isEven;

                    if (isEven)
                    {
                        holder[i].BackColor = System.Drawing.Color.Black;
                        isEven = false;
                    }
                    else
                    {
                        holder[i].BackColor = System.Drawing.Color.White;
                        isEven = true;
                    }                    
                    
                    holder[i].Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                            | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right)));
                    holder[i].SizeMode = PictureBoxSizeMode.CenterImage;
                    tlpChessboard.Controls.Add(holder[i]);
            }
            tlpChessboard.RowCount = n; // establish rows & cols
            tlpChessboard.ColumnCount = n;
            tlpChessboard.AutoSizeMode = AutoSizeMode.GrowAndShrink;
            
            tlpChessboard.Show();           
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {          
            selectBehavior();
        }

        private void btStart_Click(object sender, EventArgs e)
        {
            if (controller.IsRunning())
                controller.Stop();
            else
            {
                if (controller.IsStopped())
                    selectBehavior();

                controller.Start();
            }
        }

        private void selectBehavior()
        {
            if (!initialized)
                return;

            Win.Visible = false;
            lblMessage.Text = "";
            lblInfo.Text = "";

            lbCol.Text = 0+"";
            lbRow.Text = 0+"";

            Behaviors.Behavior behavior = Behaviors.Behavior.NQINT;

            switch (cmbModel.SelectedIndex)
            { 
                case 0:
                    behavior = Behaviors.Behavior.NQINT;
                    tbSpeed.Enabled = false;
                    btPause.Enabled = false;
                    btStart.Enabled = false;
                    break;
                case 1:
                    behavior = Behaviors.Behavior.NQBFS;
                    tbSpeed.Enabled = true;
                    btPause.Enabled = true;
                    btStart.Enabled = true;
                    break;
                case 2:
                    behavior = Behaviors.Behavior.NQDFS;
                    tbSpeed.Enabled = true;
                    btPause.Enabled = true;
                    btStart.Enabled = true;
                    break;
                case 3:
                    behavior = Behaviors.Behavior.NQMAT;
                    tbSpeed.Enabled = true;
                    btPause.Enabled = true;
                    btStart.Enabled = true;
                    break;
            }
         
                controller.SetModel(behavior, tbSpeed.Value, Convert.ToInt16(cmbSize.SelectedItem), Row, Col);
        }

        private void btPause_Click(object sender, EventArgs e)
        {
            controller.Pause();
        }

        private void cmbModel_SelectedIndexChanged(object sender, EventArgs e)
        {
            selectBehavior();
        }

        private void btUndo_Click(object sender, EventArgs e)
        {
            controller.UnsetLastPosition();
        }

        private void tbSpeed_Scroll(object sender, EventArgs e)
        {
            controller.SetSleepInterval(tbSpeed.Value);
        }

        private void ViewGUI_FormClosing(object sender, FormClosingEventArgs e)
        {
            controller.Stop();
        }

     
    }
}